import Vue from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import routerConfig from './vouter.config'
import { Storage } from '@/utils/Storage'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import packageJson from '../../package.json'


const routes = routerConfig

const router = createRouter({
  history: createWebHashHistory(''),
  routes
})

router.beforeEach((to, from, next) => {
  console.log('to', Storage.get(ACCESS_TOKEN))
  // 设置页面Title
  document.title = `${to.meta && to.meta.title} - ${packageJson.name}`;
  // 如果没有Token，并且不是登录页，就跳转到登录页
  if (to.meta.isNeedToken && !Storage.get(ACCESS_TOKEN) && to.name !== 'Login') {
    next({ path: '/' })
  }
  next()
})

export default router
